package pkg4funlex;

import java.io.BufferedReader;
import java.io.IOException;

/**
 *
 * @author 4FunTeam
 */
public class Scanner {
    private static BufferedReader inFile;
    private static String inLine;
    private static int start_lex = 0;
    private static int pos = 0;
    private static int line_index = 0;
    private static char[] line;

    public static void init(BufferedReader buff) {
        setBuffer(buff);
        nextLine();
    }
    private static void setBuffer(BufferedReader buff) {
        inFile = buff;
        line_index = 0;
        start_lex = 0;
        pos = 0;
    }
    
    public static void nextLine() {
        try {
           inLine = inFile.readLine();
           if(inLine == null) {
               line = new char[1];
               line[0] = (char) 26;
           } else {
               line = inLine.toCharArray();
               line_index++;
           }
           start_lex = 0;
           pos = 0;
        } catch (IOException ex) {
            System.out.println("num fufou" + line);
            // Logger.getLogger(Scanner.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static char getChar() {
        char c;
        try {
            c = line[pos];
            pos++;
        } catch(IndexOutOfBoundsException e) {
            c = '\n';
        }
        return c;
    }

    public static void setPos(int lex_len) {
        //System.out.println(String.valueOf(start_lex));
        if(lex_len != -1) {
            start_lex += lex_len;
            pos = start_lex;
        } else {
            nextLine();
        }
    }

    public static int getLine_index() {
        return line_index;
    }

    public static int lookForward() {
        try {
            return line[pos-1];
        } catch(IndexOutOfBoundsException e) {
            return '\n';
        }
    }
}

/*
try {
        
    String linha="";
        String nome ="";
        linha = linha.replaceAll(" ", ""); //retira os espaços

        Gramatica Gramat_original = new Gramatica();
        Gramatica Gramat_SemVazio = new Gramatica();
        Integer posicao = 0 ;
        //pegando o nome da gramatica
        while (linha.charAt(posicao) != '='){
             nome = nome + linha.charAt(posicao);
             posicao++;
        }
    }
    catch (IOException e){
        // possiveis erros são tratatos aqui
   } */